﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using STF.Blog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class FansService : IFansService
    {

        private IFansManager _ifa;

        public FansService(IFansManager ifa)
        {
            _ifa = ifa;
        }

        public async Task<int> AddFansAsync(int cid, int mid)
        {
            return await _ifa.AddAsync(new fans 
            {
                Cid = cid,
                Mid = mid
            });
        }

        public async Task<int> DeleteFansAsync(int id)
        {
            var data = await _ifa.GetById(id);
            if(data == null)
                return -1;
            if(data.DeleteId != 1)
                return -2;
            return await _ifa.DeleteAsync(data);
        }

        public async Task<List<FansDto>> GetAllFansAsync(int deleteId = 1)
        {
            var data = await _ifa.GetChooseAsync(fs => fs.DeleteId == deleteId);
            return data == null ? new List<FansDto>() : data.Select(fans => new FansDto 
            {
                Id = fans.Id,
                Mid = fans.Mid,
                Cid = fans.Cid,
                CreateTime = fans.CreateTime,
                UpdateTime = fans.UpdateTime
            }).ToList();
        }

        public async Task<FansDto> GetByIdAsync(int id)
        {
            var data = await _ifa.GetById(id);
            return data == null ? new FansDto() : new FansDto 
            {
              Id = data.Id,
              Cid = data.Cid,
              Mid = data.Mid,
              CreateTime = data.CreateTime,
              UpdateTime = data.UpdateTime
            };
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ifa.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ifa.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ifa.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ifa.UpdateAsync(data);
        }
    }
}
